package main

import "strings"

func multiply(num1 string, num2 string) string {
	if num1 == "0" || num2 == "0" {
		return "0"
	}
	var result []byte
	for i := range num1 {
		for j := range num2 {
			b1 := num1[len(num1)-1-i]
			b2 := num2[len(num2)-1-j]
			n1 := b1 - '0'
			n2 := b2 - '0'
			multi := n1 * n2
			multiBit1 := multi % 10 // 低位
			multiBit2 := multi / 10 // 高位
			pos1 := i + j
			pos2 := pos1 + 1
			if len(result) < pos2+2 {
				buf := make([]byte, pos2+2-len(result))
				result = append(result, buf...)
			}
			result[pos1] += byte(multiBit1)
			if result[pos1] >= 10 {
				result[pos1] -= 10
				result[pos1+1] += 1
			}
			result[pos2] += byte(multiBit2)
			if result[pos2] >= 10 {
				result[pos2] -= 10
				result[pos2+1] += 1
			}
		}
	}
	sb := strings.Builder{}
	i := len(result) - 1
	for ; i >= 0; i-- {
		v := result[i]
		if v != 0 {
			break
		}
	}
	for ; i >= 0; i-- {
		v := result[i]
		sb.WriteByte(v + '0')
	}
	return sb.String()
}

func main() {
	println(multiply("123456", "456"))
}
